package de.hauck.rtmidlet.model;

import java.util.Date;
import java.util.Vector;

import de.hauck.rtmidlet.utils.MyLogger;
import de.hauck.rtmidlet.utils.Utils;

/**
 * JavaBean for stocks
 * 
 * @author Achim Hauck
 */
public class StockBean {

	private String name;
	private String[] chartSymbols;
	private String symbol_yahoo;
	private String amount;
	private String invest;
	private Date buyDate;
	private boolean[] displayFlags;
	private boolean showInYahoo;
	private int chartSymbolsCount = 0;
//	private int posYahoo;
//	private int posAmount;
//	private int posInvestment;
//	private int posBuyDate;
//	private int posDisplayFlags;

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String[] getChartSymbols() {
		return chartSymbols;
	}

	public void setChartSymbols(String[] symbols) {
		this.chartSymbols = symbols;
	}

	public String getChartSymbol(int i) {
		if (i >= chartSymbols.length)
			return "";
		else
			return chartSymbols[i];
	}

	public void setChartSymbol(int i, String symbol) {
		if (i < chartSymbols.length)
			chartSymbols[i] = symbol;
		else
			System.out.println("array not long enough");
	}

	public String getSymbolYahoo() {
		return this.symbol_yahoo;
	}

	public void setSymbolYahoo(String symbol_yahoo) {
		this.symbol_yahoo = symbol_yahoo;
	}

	public String getAmount() {
		return this.amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public String getInvest() {
		return this.invest;
	}

	public void setInvest(String invest) {
		this.invest = invest;
	}

	public Date getBuyDate() {
		return buyDate;
	}

	public void setBuyDate(Date buyDate) {
		this.buyDate = buyDate;
	}

	public boolean[] getDisplayFlags() {
		return this.displayFlags;

	}

	public void setDisplayFlags(boolean[] displayFlags) {
		this.displayFlags = displayFlags;
	}

	/**
	 * This constructor creates a new JavaBean with given arguments
	 * 
	 * @return
	 */
	public StockBean(String name, String symbol_rt, String symbol_ts,
			String symbol_yahoo, String symbol_ft, String amount,
			String invest, Date buyDate, boolean[] displayFlags) {
		this.name = name;
		String[] chartArray = { symbol_rt, symbol_ts, symbol_ts, symbol_ft };
		this.chartSymbols = chartArray;
		this.symbol_yahoo = symbol_yahoo;
		this.amount = amount;
		this.invest = invest;
		this.buyDate = buyDate;
		this.displayFlags = displayFlags;
	}

	public StockBean(String name, String[] symbols, String symbol_yahoo,
			String amount, String invest, Date buyDate, boolean yahoo,
			boolean[] displayFlags) {
		this.name = name;
		this.chartSymbols = symbols;
		this.symbol_yahoo = symbol_yahoo;
		this.amount = amount;
		this.invest = invest;
		this.buyDate = buyDate;
		this.showInYahoo = yahoo;
		this.displayFlags = displayFlags;
	}

	public StockBean() {

	}

	/**
	 * setToken sets the fields/token of a StockBean dependent from their
	 * position i. 0: sets Name 1: sets chartSymbolsCounter 2 -
	 * (2+chartSymbolsCounter): sets chartSymbols 3: sets Symbol Yahoo 4: sets
	 * symbol FT 5: sets amount 6: sets investment 7: sets buy date 8: sets
	 * display flags
	 * 
	 */
	public void setToken(int i, String token) {
		// System.out.println(i + ": " + token);
		switch (i) {
		case 0:
			setName(token);
			break;
		case 1: {
			chartSymbolsCount = Integer.valueOf(token).intValue();
			chartSymbols = new String[chartSymbolsCount];
			break;
		}
		default: {
			if ((chartSymbolsCount > 0) && (i > 1)
					&& (i < (2 + chartSymbolsCount))) {
				// System.out.println("chartSymbol[" + (i - 2) + "]: " + token);
				chartSymbols[i - 2] = token;
			} else {
				// System.out.println("pos: " + i + ": " + token);
				if (i == chartSymbolsCount + 2) {
					setSymbolYahoo(token);
				} else if (i == chartSymbolsCount + 3) {
					setAmount(token);
				} else if (i == chartSymbolsCount + 4) {
					setInvest(token);
				} else if (i == chartSymbolsCount + 5) {
					setBuyDate(Utils.convertString2Date(token));
				} else if (i == chartSymbolsCount + 6) {
					if (token.toLowerCase().equals("true"))
						setShowInYahoo(true);
					else if (token.toLowerCase().equals("false"))
						setShowInYahoo(false);
				} else if (i == chartSymbolsCount + 7) {
					setDisplayFlags(Utils.convertString2DisplayFlags(token));
				}
			}
		}

		}
	}

	public void printBean() {
		String bean = ("Bean: [\"" + name + "\", \"" + chartSymbolsCount + "\", ");
		for (int i = 0; i < chartSymbolsCount; i++) {
			bean = bean + "\"" + chartSymbols[i] + "\", ";
		}
		bean = bean + "\"" + symbol_yahoo + "\", " + "\"" + amount + "\", "
				+ "\"" + invest + "\", " + "\""
				+ Utils.convertDate2String(buyDate) + "\", " + "\""
				+ Utils.convertDisplayFlags2String(displayFlags) + "\"]";
		MyLogger.debug(bean);
	}

	public int compareTo(Object o) {
		return name.toUpperCase().compareTo(
				((StockBean) o).getName().toUpperCase());
	}

	public boolean getShowInYahoo() {
		return showInYahoo;
	}

	public void setShowInYahoo(boolean arg) {
		showInYahoo = arg;
	}

	public void deleteEntriesAt(int pos) {
		if (pos < displayFlags.length) {
			boolean[] newDisplayFlag = new boolean[displayFlags.length - 1];
			int newPos = 0;
			for (int i = 0; i < displayFlags.length; i++) {
				if (pos != i) {
					newDisplayFlag[newPos] = displayFlags[i];
					newPos++;
				}
			}
			displayFlags = new boolean[newDisplayFlag.length];
			displayFlags = newDisplayFlag;
		} else
			MyLogger.debug("No entry at position " + pos + " for " + getName()
					+ " to delete in DisplayFlagsArray");

		if (pos < chartSymbols.length) {
			String[] newChartSymbols = new String[chartSymbols.length - 1];
			int newPos = 0;
			for (int i = 0; i < chartSymbols.length; i++) {
				if (pos != i) {
					newChartSymbols[newPos] = chartSymbols[i];
					newPos++;
				}
			}
			chartSymbols = new String[newChartSymbols.length];
			chartSymbols = newChartSymbols;
		} else
			MyLogger.debug("No entry at position " + pos + " for " + getName()
					+ " to delete in chartSymbolArray");

	}
	
	public void addEntry(boolean entry) {
		boolean[] newDisplayFlags = new boolean[displayFlags.length+1];
		for (int i=0;i<displayFlags.length; i++) {
			newDisplayFlags[i]=displayFlags[i];
		}
		newDisplayFlags[displayFlags.length]=entry;
		displayFlags=new boolean[newDisplayFlags.length];
		displayFlags=newDisplayFlags;
	}

}
